package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.NumberRange;
import com.shrct.businessprocess.project.RCSalePayStrategy;
import com.shrct.businessprocess.project.proxy.RCSalePayStrategyProxy;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALEPAYSTRATEGY_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalepaystrategy")
public class RCSalePayStrategyImpl extends BaseModelObject implements RCSalePayStrategy {
  public RCSalePayStrategyImpl() {
    super();
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "fromValue", column = @Column(name = "range_fromValue") ), @AttributeOverride(name = "toValue", column = @Column(name = "range_toValue") ), @AttributeOverride(name = "workaround", column = @Column(name = "range_workaround") ) } )
  @AssociationOverrides(value = { } )
  private NumberRange range = com.shrct.businessprocess.project.NumberRange.create();
  
  public NumberRange getRange() {
    return this.range;
  }
  
  public RCSalePayStrategy setRange(final NumberRange range) {
    this.range = range;
    return this;			
    
  }
  
  private Double com1Rate;
  
  public Double getCom1Rate() {
    return this.com1Rate;
  }
  
  public RCSalePayStrategy setCom1Rate(final Double com1Rate) {
    this.com1Rate = com1Rate;
    return this;			
    
  }
  
  public Double getCom2Rate() {
    double _minus = (1 - (this.com1Rate).doubleValue());
    return Double.valueOf(_minus);
  }
  
  public void update(final Procedure1<? super RCSalePayStrategy> updater) {
    updater.apply(this);
  }
  
  public Double calcCom2Amount(final Double totalAmount, final Double comparableIncome) {
    double _xblockexpression = (double) 0;
    {
      Double _fromValue = this.range.getFromValue();
      double _multiply = DoubleExtensions.operator_multiply(comparableIncome, _fromValue);
      final double fromPart = Math.min((totalAmount).doubleValue(), _multiply);
      Double _xifexpression = null;
      Double _toValue = this.range.getToValue();
      boolean _equals = Objects.equal(_toValue, null);
      if (_equals) {
        _xifexpression = totalAmount;
      } else {
        Double _toValue_1 = this.range.getToValue();
        double _multiply_1 = DoubleExtensions.operator_multiply(comparableIncome, _toValue_1);
        double _min = Math.min((totalAmount).doubleValue(), _multiply_1);
        _xifexpression = _min;
      }
      final Double toPart = _xifexpression;
      double _minus = ((toPart).doubleValue() - fromPart);
      Double _com2Rate = this.getCom2Rate();
      double _multiply_2 = (_minus * (_com2Rate).doubleValue());
      _xblockexpression = (_multiply_2);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public static RCSalePayStrategy create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSalePayStrategy rCSalePayStrategy = new com.shrct.businessprocess.project.impl.RCSalePayStrategyImpl(
    );
    objectFactory.create(rCSalePayStrategy);
    return rCSalePayStrategy;			
    
  }
  
  public static RCSalePayStrategy createRCSalePayStrategyImpl(final IObjectFactory objectFactory, final Procedure1<RCSalePayStrategy> init) {
    com.shrct.businessprocess.project.RCSalePayStrategy rCSalePayStrategy = new com.shrct.businessprocess.project.impl.RCSalePayStrategyImpl(
    );
    objectFactory.create(rCSalePayStrategy);
    init.apply(rCSalePayStrategy);
    
    return rCSalePayStrategy;			
    
  }
  
  public RCSalePayStrategyProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSalePayStrategyProxy proxy = new com.shrct.businessprocess.project.proxy.RCSalePayStrategyProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
